package com.dianrui.fmi.activity;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.SPUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.im.android.api.JMessageClient;


public class SettingsActivity extends BaseActivity {
    boolean isSwitch = true;
    int flag=0;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.current_language)
    TextView currentLanguage;
    @BindView(R.id.switch_msg_reminder)
    Switch switchMsgReminder;


    @Override
    public int getContentViewId() {
        return R.layout.about_settings;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.setting));
        initConfig();

        switchMsgReminder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    switchMsgReminder.setChecked(true);
                    MessageReminder("1");
                }else {
                    switchMsgReminder.setChecked(false);
                    MessageReminder("0");
                }
            }
        });
    }


    private void initConfig() {
        if (spUtils == null) {
            spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        }
        if (!StringUtils.isEmpty(spUtils.getString("language_flags"))) {
            switch (spUtils.getString("language_flags")) {
                case "one":
                    currentLanguage.setText(getResources().getString(R.string.english));
                    break;
                case "two":
                    currentLanguage.setText(getResources().getString(R.string.burmese));
                    break;

                default:
                    break;
            }

        }
    }

    @OnClick({R.id.back, R.id.logout, R.id.pwd_layout, R.id.lanuage_layout,R.id.msg_reminder_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.lanuage_layout:
                JumpActivitys(ChangeLanguageActivity.class);
                break;
            case R.id.pwd_layout:
                JumpActivitys(UpdatePasswordActivity.class);
                break;
            case R.id.logout:
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this, R.style.RcDialog);
                View view1 = LayoutInflater.from(SettingsActivity.this).inflate(R.layout.login_out_ask, null);
                final AlertDialog dialog = builder.create();
                dialog.setView(view1);
                TextView cancel_dialog_ask = view1.findViewById(R.id.cancel_dialog_ask);
                final TextView yes_dialog_ask = view1.findViewById(R.id.yes_dialog_ask);
                cancel_dialog_ask.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                yes_dialog_ask.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        spUtils.clear();
                        JMessageClient.logout();
                        JumpLoginActivitys(LoginActivity.class);
                    }
                });
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();

                break;
            case R.id.msg_reminder_layout:
                if(flag == 0) {
                    switchMsgReminder.setChecked(true);
                    flag=1;
                    MessageReminder("1");
                }else {
                    flag=0;
                    switchMsgReminder.setChecked(false);
                    MessageReminder("0");
                }

                break;

            default:
                break;
        }
    }

    private void MessageReminder(String isPush) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("is_push", isPush);
        OkHttpUtils.getInstance().postWithMemberToken(Url.SETINFO, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));

                            } else {

                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }



}
